package com.terabits.smartriver.job;

import com.terabits.smartriver.constants.ModelEnum;
import com.terabits.smartriver.mapper.DeviceMapper;
import com.terabits.smartriver.mapper.ModelMapper;
import com.terabits.smartriver.meta.po.Device;
import com.terabits.smartriver.service.CalculationService;
import com.terabits.smartriver.service.impl.BaseService;
import com.terabits.smartriver.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.terabits.smartriver.util.DateUtils.getDateOnlyYMD;


@Component
public class CheckDevice extends BaseService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private CalculationService calculationService;

    //    @Scheduled(cron = "0 30 10 ? * *")
    @Scheduled(cron = "0 */5 * * * ?")
    public void CheckDeviceByDate() {

        //获取所有设备
        List<Device> devices = deviceMapper.selectAll();
        for (Device device : devices) {
            Date time_lastreport = device.getTimeLastreport();
            String data = device.getValueLast();
            if (ObjectUtils.isEmpty(data) || ObjectUtils.isEmpty(time_lastreport)) {
                //修改设备状态为离线状态
                Device update = new Device();
                update.setState(ModelEnum.level_offline.getCode());
                Example example = new Example(Device.class);
                example.createCriteria().andEqualTo("deviceNum", device.getDeviceNum());
                deviceMapper.updateByExampleSelective(update, example);
                System.out.println("设备暂无数据---");
                continue;
            }
            SimpleDateFormat localTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String before3Day = getDateOnlyYMD(null, null, -1);
//            String nowDay = getDateOnlyYMD(null, null, -0);
            Date sdate = null;
//            Date edate = null;
            Date date = null;
            try {
                sdate = localTime.parse(before3Day);
//                edate = localTime.parse(nowDay);
                date = time_lastreport;
                if (date.getTime() <= sdate.getTime()) {
                    //修改设备状态为离线状态
                    Device update = new Device();
                    update.setState(ModelEnum.level_offline.getCode());
                    Example example = new Example(Device.class);
                    example.createCriteria().andEqualTo("deviceNum", device.getDeviceNum());
                    deviceMapper.updateByExampleSelective(update, example);
                    System.out.println("设备最新上报时间超过一天---");
                } else {
                    //todo
                    //这里不能这么写，因为checkDevice这个定时任务是对设备进行检查，checkModel这个定时是对绑定模型的设备进行检查
                    //所以设备的状态显示可能有时候不正确，打比方，设备离线了，按理来说应该显示离线状态，可是这个设备绑定了模型，
                    // checkModel定时任务跑完之后可能把设备状态改成了正常或者异常
                    //正确的做法是把checkModel定时任务放在这里的else里面，先判断设备状态是不是正常，不是正常改成正常，然后再去进行checkModel里面的逻辑。
                    // 这样才符合正常逻辑，只对在线的设备进行模型绑定检查

                    //逻辑已做了修改
                    //对正常状态的设备并且绑定了模型的设备进行模型绑定检查
                    if (device.getState() != 0) {
                        Device update = new Device();
                        update.setState(ModelEnum.level_normal.getCode());
                        Example example = new Example(Device.class);
                        example.createCriteria().andEqualTo("deviceNum", device.getDeviceNum());
                        deviceMapper.updateByExampleSelective(update, example);
                    }
                    PageData pageData = new PageData();
                    pageData.put("devices", device.getDeviceNum());
                    List<PageData> models = modelMapper.selectModelDeviceByDeviceId(pageData);
                    if (models.size() > 0) {
                        calculationService.checkSingleModel(models.get(0));
                        System.out.println("设备模型绑定检查完成---");
                        continue;
                    }
                    System.out.println("设备显示正常---");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}
